const model = require('../models/BaseModel.js')
const { SHOPCART } = require('../config/tableConfig.js')
const { ADDRESS } = require('../config/tableConfig.js')
const { PRODUCTFIELD } = require('../fields/productField.js')
const { CARTFIELD } = require('../fields/cartField.js')
const {ADDRESSFIELD } = require('../fields/addressField.js')
/**
 * 添加购物车
 * @param addshop 条件
 * @return 
 */
const addCartService = (addshop) => {
  console.log(addshop)
  let options = {}
  options._id = addshop._id
  model.query(SHOPCART,PRODUCTFIELD, options).then((v)=>{
    console.log(v)
    if(v.data.length === 0) {
      return model.add(SHOPCART, addshop)
    }else{
      console.log('进入')
      return model.update(SHOPCART, addshop._id, "num", addshop.num)
    }
  })
}
/**
 * 获取购物车商品(全部)
 * @return 
 */
const getCartService = () =>{
  return model.query(SHOPCART,CARTFIELD)
}
/**
 * 获取购物车商品(id)
 * @return 
 */
const getByIdService = (addshopId) =>{
  console.log(addshopId)
  return model.query3(SHOPCART, CARTFIELD, addshopId)
}
/**
 * 删除购物车商品
 * @return 
 */
const removeCartService = (id) =>{
  console.log(id)
  return model.removeMultiple(SHOPCART,id)
}
/**
 * 更新购物车商品
 * @return 
 */
const updatedCartService = (ids) =>{
  console.log(ids)
  if(ids.num){
    console.log('进入1')
    return model.update(SHOPCART, ids._id, "num", ids.num)
  }else{
    console.log('进入2')
    return model.update(ADDRESS, ids._id, "default", ids.default)
  }
}
/**
 * 添加收货地址
 * @return 
 */
const addAddressService = (address) =>{
  console.log(address)
  return model.add(ADDRESS, address)
}
/**
 * 获取收货地址(全部)
 * @return 
 */
const getAdressService = () =>{
  return model.query(ADDRESS,ADDRESSFIELD)
}
/**
 * 更新收货地址
 * @return 
 */
const updatedAddressService = (ids) =>{
  console.log(ids)
  return model.update1(ADDRESS, ids[0], ids[1])
}
/**
 * 删除收货地址
 * @return 
 */
const removeAddressService = (id) =>{
  console.log(id)
  return model.removeSingle(ADDRESS,id)
}

module.exports = {
  addCartService,
  getCartService,
  removeCartService,
  updatedCartService,
  getByIdService,
  addAddressService,
  getAdressService,
  updatedAddressService,
  removeAddressService
}